/*
 * Project: yygh-parent
 *
 * File Created at 2023/7/10
 *
 * 自定义时间格式代替DATE标签：2023-07-10 19:21:19:21
 */
package com.atguigu.yygh.order.service.impl;

import com.alibaba.fastjson.JSONObject;

import com.atguigu.yygh.DictFeignClient;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.repository.HospitalRepository;
import com.atguigu.yygh.order.service.HospitalService1;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author 用户名
 * @Type HospitalServiceImpl.java
 * @Desc
 * @date 2023/7/10 19:21
 */
@Slf4j
@Service
public class HospitalService1Impl implements HospitalService1 {
    @Autowired
    HospitalRepository hospitalRepository;
    @Autowired
    DictFeignClient dictFeignClient;


    //上传医院
    @Override
    public void save(Map paramMap) {
        log.info(JSONObject.toJSONString(paramMap));
        Hospital hospital = JSONObject.parseObject(JSONObject.toJSONString(paramMap),Hospital.class);
        Hospital target = hospitalRepository.getHospitalByHoscode(hospital.getHoscode());
        String logoDataString = (String)paramMap.get("logoData");
//        if(!StringUtils.isEmpty(logoDataString)) {
//            String logoData = logoDataString.replaceAll(" ", "+");
//            paramMap.put("logoData", logoData);
//        }
        if(null!=target){
            hospital.setStatus(target.getStatus());
            hospital.setCreateTime(target.getCreateTime());
            hospital.setUpdateTime(target.getUpdateTime());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }else{
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }



    }

    @Override
    public Hospital findByHosCode(Map<String, Object> map) {
        String hoscode=(String)map.get("hoscode");
       return hospitalRepository.getHospitalByHoscode(hoscode);

    }

    @Override
    public Page selectPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {


        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
//0为第一页
        Pageable pageable = PageRequest.of(page-1, limit, sort);

        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo, hospital);

//创建匹配器，即如何使用查询条件
        ExampleMatcher matcher = ExampleMatcher.matching() //构建对象
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //改变默认字符串匹配方式：模糊查询
                .withIgnoreCase(true); //改变默认大小写忽略方式：忽略大小写

        //创建实例
        Example<Hospital> example = Example.of(hospital, matcher);
        Page<Hospital> pages = hospitalRepository.findAll(example, pageable);
        for (Hospital page1 : pages) {
          page1.setHostype(this.getName(Integer.valueOf(page1.getHostype()), 10000));
        }

        return pages;
    }
    //得到医院的类型
    @Override
    public String getName(Integer value, Integer parentId) {
       String type= dictFeignClient.getNameByValueAndParentId(value,parentId);

        return type;
    }
    //得到医院详细的地址
    @Override
    public String getDetailAddress(Integer provinceCode, Integer cityCode,Integer districtCode) {
        String type= dictFeignClient.getDetailAddress(provinceCode,cityCode,districtCode);
        return type;
    }

    @Override
    public void updateStatusById(String id, Integer status) {
        if(status !=0 && status !=1){
            return;
        }
        Hospital hospital=hospitalRepository.findHospitalById(id);
        hospital.setStatus(status);

        hospitalRepository.save(hospital);

    }

    @Override
    public Hospital selectHospitalById(String id) {
        Hospital hospital=hospitalRepository.findHospitalById(id);
        if(hospital!=null){
            String detailAddress =
                    this.getDetailAddress(Integer.parseInt(hospital.getProvinceCode()),
                            Integer.parseInt(hospital.getCityCode()), Integer.parseInt(hospital.getDistrictCode())
                    );
            hospital.setAddress(detailAddress+hospital.getAddress());
            String name = this.getName(Integer.parseInt(hospital.getHostype()), 10000);
            Map<String, Object> param = hospital.getParam();
            param.put("bookingRule",hospital.getBookingRule());
            param.put("hostypeString",name);

        }

        //处理预约规则方便前端调用

        hospital.setBookingRule(null);


        return hospital;
    }

    @Override
    public List<Hospital> findByHosname(String hosname) {

        List<Hospital> hospitalByHosnameLike = hospitalRepository.findHospitalByHosnameLike(hosname);
        ArrayList<Hospital> objects = new ArrayList<>();
        for (Hospital hospital : hospitalByHosnameLike) {
            Hospital hospital1 = new Hospital();
            BeanUtils.copyProperties(hospital,hospital1);
            String name = this.getName(Integer.valueOf(hospital.getHostype()), 10000);
            System.out.println("-----name-----"+name);
            hospital1.setHostype(name);
            hospital1.getParam().put("bookingRule",hospital.getBookingRule());
            objects.add(hospital1);
        }
        return objects;
    }

    @Override
    public Map<String,Object> getHospitalByHoscode(String hoscode) {
        Map<String, Object> map = new HashMap<>();
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        String name = this.getName(Integer.valueOf(hospital.getHostype()), 10000);
        hospital.setHostype(name);
        map.put("hospital",hospital);
        map.put("bookingRule",hospital.getBookingRule());
        hospital.setBookingRule(null);
        return map;
    }


}


/**
 * Revision history
 * -------------------------------------------------------------------------
 * <p>
 * Date Author Note
 * -------------------------------------------------------------------------
 * 2023/7/10 用户名 create
 */ 
